<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id="i1" />
<input type="text" id="i2" />
<input type="button" value="加" onclick="f(1)"/>
<input type="button" value="减" onclick="f(2)"/>
<input type="button" value="乘" onclick="f(3)"/>
<input type="button" value="除" onclick="f(4)"/>
<h3>
  结果：<span></span>
</h3>
</body>
<script>
  let i1 = document.querySelector("#i1");
  let i2 = document.querySelector("#i2");
  let s = document.querySelector("span");
  function f(cNum){
    let n1 = i1.value;
    let n2 = i2.value;
    if(isNaN(n1) || isNaN(n2)){
      s.innerText ="输入错误";
      return;
    }
    n1 = parseFloat(n1);
    n2 = n2*1;
    let result;
    switch (cNum) {
      case 1:
        result = n1+n2;break;
      case 2:
        result = n1-n2;break;
      case 3:
        result = n1*n2;break;
      case 4:
        result = n1/n2;break;
    }
    s.innerText = result;
  }

</script>
</html>